﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

public partial class Control_datePicker : controlMatrix //System.Web.UI.UserControl
{
    protected string errorMSG = "";
    protected bool isEmpty = true;

    /// <summary>
    /// 如果沒有輸入時，會是true
    /// </summary>
    public bool IsEmpty
    {
        get
        {
            Validate();
            return isEmpty;
        }
    }

    /// <summary>
    /// 當Validate 時有錯誤會存在這
    /// </summary>
    public string ErrorMSG
    {
        get
        {
            return errorMSG;
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {

        Utility.LoadJquery(this, "jquery", "jquery");
        Utility.LoadJquery(this, "jqueryUI", "jqueryUI");
           

        string script = @" $(function()
        {
            $('.date-pick').datepicker(
                    { buttonText: '...'
                     , showOn: 'button'
                     , dateFormat: 'yy-mm-dd'                     
                     }
                );
            $('.date-pick').attr('size', '10');
        });";
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "datePick", script, true);
    }

    /// <summary>
    /// 取回日期
    /// </summary>
    /// <returns></returns>
    public string GetStringDate()
    {
        return Validate() ? txt_date.Text : string.Empty;
    }

    /// <summary>
    /// 確認日期格式是否正確
    /// </summary>
    /// <returns></returns>
    public bool Validate()
    {
        bool ok = true;
        isEmpty = true;
        errorMSG = string.Empty;
        if (txt_date.Text != string.Empty)
        {
            isEmpty = false;
            try
            {
                Convert.ToDateTime(txt_date.Text);
            }
            catch (Exception ex)
            {
                errorMSG = ex.Message.ToString();
                ok = false;
            }
        }
        return ok;
    }

    public void SetDate(string p_date)
    {
        txt_date.Text = p_date;
        if (!Validate())
        {
            txt_date.Text = string.Empty;
        }
    }
    
}
